package com.imisty.service.impl;

import com.imisty.bean.Person;
import com.imisty.dao.PersonMapper;
import com.imisty.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class UserServiceImpl implements UserService {

    public static SqlSessionFactory sqlSessionFactory = null;

    public static SqlSessionFactory getSqlSessionFactory() {
        if (sqlSessionFactory == null) {
            String resource = "mybatis-config.xml";
            try {
                Reader reader = Resources.getResourceAsReader(resource);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sqlSessionFactory;
    }

    public List<Person> getAllUserByPageHelper() {
        SqlSession session = getSqlSessionFactory().openSession();
        PersonMapper personMapper = session.getMapper(PersonMapper.class);

        List<Person> persons = personMapper.getAllPersons();
        return persons;
    }
}
